// pages/keepcar/adcarsrv.js
var util = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    listInfo: [],
    showModalStatus: false,
    showTopTips: false,
    errorMsg: '',
    orderlists: [],
    planlists: [],
    loading: true,
    userinfo: '',
    uid: 0,
    oid: 0, //orderid
    cf: '',
    jjreason: '',
    isshow: false,
    showModalImg: false,
    animationData: '',
    carpics: [],
    carUpload: true,
    statu:0,
    imgtype:'',
    showtitle:'拍照验收'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.globalData.isLoginAuth) {
      this.showModal();
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    if (app.globalData.isLoginAuth) {
      this.setData({
        uid: wx.getStorageSync('userId'),
        listInfo: app.globalData.listInfo
      })
      this.getApplylist();
    }
  },
  getApplylist: function () {
    var that = this;
    var identity = wx.getStorageSync('roleFlag');
    wx.request({
      url: app.globalData.hostUrl + 'member/applyForsvrer',
      data: {
        identity: identity,
        uid: wx.getStorageSync('userId')
      },
      methods: 'GET',
      header: {
        "Content-Type": "json"
      },
      success: function (res) {
        console.log(res);
        if (res.data.result) {
          that.setData({
            orderlists: res.data.result.lists,
          });
        }
        else {
          that.setData({
            loading: false
          })
          return false
        }
      }
    })
  },
  choosecarImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 4,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          carpics: that.data.carpics.concat(res.tempFilePaths)
        });
        //上传图片
        app.uploadimg({
          url: app.globalData.hostUrl + 'member/applyUpload', //这里是你图片上传的接口
          path: tempFilePaths, //这里是选取的图片的地址数组
          imgtype: that.data.imgtype,
          rid: that.data.oid
        });

        if (that.data.carpics.length > 3) {
          that.setData({
            carUpload: false
          });
        }
      }
    })
  },
  // 删除图片
  deleteImg: function (e) {
    if (this.data.mid > 0) {
      return false;
    }
    var that = this;
    var name = e.currentTarget.dataset.name;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    var imgfiles = this.data.carpics;

    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          var str = imgfiles[index];
          var imgName = str.substring(str.lastIndexOf("/") + 1);
          //console.log(imgName);
          wx.request({
            url: app.globalData.hostUrl + 'member/Delimage', //删除上传图片
            data: {
              imgname: imgName
            },
            success: function (res) {
              //              console.log(res);
              if (res.data.code == 200) {
                imgfiles.splice(index, 1);
                that.setData({
                  carpics: imgfiles,
                  carUpload: true
                });
              }
            }
          })

        } else if (res.cancel) {
          console.log('点击取消删除');
          return false;
        }

      }
    })
  },
  previewImage: function (e) {
    var imgfiles = null;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    imgfiles = this.data.carpics;

    var imgurl = [imgfiles[index]];
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: imgurl // 需要预览的图片http链接列表
    })
  },
  calling: function (e) {
    var mobile = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: mobile,
      success: function () {
        //console.log("拨打电话成功！")
      },
      fail: function () {
        // console.log("拨打电话失败！")
      }
    })
  },
  getreason: function (e) {
    this.setData({
      jjreason: e.detail.value
    });
  },
  onRefuse: function () {
    var that = this;
    var id = that.data.oid;
    var cf = that.data.cf;
    var reason = that.data.jjreason;
    var finish = -1;
    if (reason == '') {
      wx.showToast({
        title: '请填写拒绝理由',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    wx.showModal({
      content: '您确定拒绝本订单吗?',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.hostUrl + 'member/modifyStatus',
            data: {
              flag: cf,
              fval: finish,
              id: id,
              reason: reason
            },
            methods: 'GET',
            success: function (res) {
              if (res.data.code == 200) {
                that.setData({
                  isshow: true
                })
                wx.navigateTo({
                  url: '../keepcar/adcarsrv',
                })
              }
              else {
                wx.showToast({
                  title: '操作失败',
                  icon: 'error',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    });
  },
  taskimg:function(){
    var that = this;
    var id = that.data.oid;
    var cf = that.data.cf;
    var finish = that.data.statu;
    var imgtype = that.data.imgtype;
    wx.request({
      url: app.globalData.hostUrl + 'member/takeimage',
      data: {
        imgtype: imgtype,
        id: id,
        flag: cf,
        fval: finish,
      },
      methods: 'GET',
      success: function (res) {
        if (res.data.code == -1) {
          wx.showModal({
            title: '提示',
            content: res.data.msg
          });
        }
        else if (res.data.code == 200) {
          that.setData({
            carUpload: false,
            carpics: []
          });
          wx.navigateTo({
            url: '../keepcar/adcarsrv',
          })
        }
        else {
          wx.showToast({
            title: '操作失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
    
  },
  modifyfinish: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.oid;
    var cf = e.currentTarget.dataset.cf;
    var finish = e.currentTarget.dataset.finish;
    wx.showModal({
      content: '您确定此次操作吗?',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.hostUrl + 'member/modifyStatus',
            data: {
              flag: cf,
              fval: finish,
              id: id
            },
            methods: 'GET',
            success: function (res) {
              if (res.data.code == 200) {
                that.setData({
                  isshow: true
                })
                wx.navigateTo({
                  url: '../keepcar/adcarsrv',
                })
              }
              else {
                wx.showToast({
                  title: '操作失败',
                  icon: 'error',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    });
  },
  goorderdetail: function (e) {
    var rid = e.currentTarget.dataset.id;
    var cf = e.currentTarget.dataset.cf;
    var gotourl = '../keepcar/carsrvdetail?oid=' + rid + '&cf=' + cf;
    if (cf == 'bz' || cf == 'hz')
      gotourl = '../keepcar/paperdetail?oid=' + rid + '&cf=' + cf;
    wx.navigateTo({
      url: gotourl
    })
  },
  // 弹框
  openEdit: function (e) {
    this.setData({
      showModalStatus: true,
      oid: e.currentTarget.dataset.oid,
      cf: e.currentTarget.dataset.cf
    });
    util.clearError(this);
  },
  //關閉
  closeEdit: function (e) {
    this.setData({
      showModalStatus: false
    });
    util.clearError(this);
  },
  openImg: function (e) {
    var that=this;
    var imgtype = e.currentTarget.dataset.imgtype
    this.setData({
      showModalImg: true,
      oid: e.currentTarget.dataset.oid,
      cf: e.currentTarget.dataset.cf,
      statu: e.currentTarget.dataset.finish,
      imgtype: imgtype,
      carpics:[],
      showtitle: imgtype == 'wanchengimg' ? '拍照完成' :'拍照验收'
    });
    wx.request({
      url: app.globalData.hostUrl + 'member/getcarimg',
      data: {
        imgtype: imgtype,
        id: e.currentTarget.dataset.oid,
      },
      methods: 'GET',
      success: function (res) {
        console.log(res);
        var len = res.data.result.length;
        for (var i = 0; i < len;i++){
          that.setData({
            carpics: that.data.carpics.concat(res.data.result[i].url),
          });
        }
        if(len==0){
          that.setData({
            carUpload: true
          });
        }
        if (len == 4) {
          that.setData({
            carUpload: false
          });
        }
      }
    })
    util.clearError(this);
  },
  imgEdit: function (e) {
    this.setData({
      showModalImg: false
    });
    util.clearError(this);
  },
  gohome: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  yuy: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../keepcar/mycarsrv',
      })
    }
    else {
      wx.redirectTo({
        url: '../keepcar/adcarsrv',
      })
    }
  },
  orders: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../keepcar/carsrvmanage',
      })
    }
    else {
      wx.redirectTo({
        url: '../keepcar/hiscarsrv',
      })
    }
  },
  mys: function () {
    wx.redirectTo({
      url: '../myself/myself',
    })
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.globalData.userInfo = e.detail.userInfo;
      app.getfansinfo();
    }
    else {
      wx.redirectTo({
        url: '../index/index',
      })
    }
  }
})